// const api_url = "https://wx.quanyouyun.cn/wechat";
const api_url = "https://wxtest.quanyouyun.cn/wechat";
// const api_url = "http://192.168.2.94:8093/wechat";
// const api_url = "http://192.168.2.173:8093/wechat";
const formatTime = date => {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hour = date.getHours();
    const minute = date.getMinutes();
    const second = date.getSeconds();

    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
};

const formatNumber = n => {
    n = n.toString();
    return n[1] ? n : '0' + n
}
//获取url指定参数的值
const getQueryString = function (url, name) {
    var reg = new RegExp('(^|&|/?)' + name + '=([^&|/?]*)(&|/?|$)', 'i');
    var r = url.substr(1).match(reg);
    if (r != null) {
        return r[2]
    }
    return null;
}
//请求封装
const FetchRequest = (url, data, method = 'POST', header = {}) => {
    return new Promise((resolve, reject) => {
        let _url = api_url + url;
        header = {"token": wx.getStorageSync('token')};
        wx.request({
            url: _url,
            method: method.toUpperCase(),
            data: data,
            header: header,
            success: function (res) {
                if (res.data.success) {
                    resolve(res);
                } else {
                    wx.showToast({
                        title: res.data.message,
                        icon: 'none',
                        duration: 2000
                    });
                }
            },
            fail: function (res) {
                if (res.data) {
                    wx.showToast({
                        title: res.data.message,
                        icon: 'none',
                        duration: 3000
                    })
                }
            },
            complete: function (res) {
                // 不论请求是否成功  都会走个方法
            }
        })
    });
};
//脱敏
const desensitization = function (str, beginLen, endLen) {
    var lastStr = '';
    var len = str.length;
    var firstStr = str.substr(0, beginLen);
    if (endLen) {
        lastStr = str.substr(endLen);
    }
    var middleStr = str.substring(beginLen, len - Math.abs(endLen)).replace(/[\s\S]/ig, '*');
    var tempStr = firstStr + middleStr + lastStr;
    return tempStr;
}

module.exports = {
    formatTime: formatTime,
    getQueryString: getQueryString,
    FetchRequest: FetchRequest,
    api_url: api_url,
    desensitization: desensitization
}
